<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-11
 * Time: 13:11
 */

namespace app\common\model;
include_once VENDOR_PATH.'alipaysdk/AopSdk.php';
class Alipay extends Base
{

  public $api_url = 'https://mapi.alipay.com/gateway.do?';
  public $ALIPAY_PAYKEY = '91kspg9zt7q6qw50tmugy00e5dc6e14x';//淘酒人合伙伙伴密匙
  public $partner = '2088721275451868';//支付宝合作者身份ID 2088开头
  public $aop;
  
  public function __construct()
  {
      parent::__construct();
      $aop = new \AopClient ();
      $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
      $aop->appId = '2018032702456663';
      $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAyKLZnZgcXIQaJwmW65Vqp3BH4MTjO86/d6Z6YFwuXK3ZzxGujefTTV4hMKl0aG94c6WXsWHppYggYMmXz5NSki+4nB/fPflEPGk0fwdNZ1e7CROnd7NIHJ8R1lT8V5z4rCbgX0EsxP5GNaUu77cPxUDWspzzEIkYDiKbBw0EIKSBGYG9KhqsmEa8mNp5VwPH8BQTcfxyFFRieP0woo/MvLN/Tvj53nQJxGxvGqOB88G6obYPjYZIa8H3U8kS6TRlCPPJ0KpRL+EZo+EosY/Avvp/jyhmFv1KzGXX1oBGrPWhADPqYn2/rKHOX9atV1xCJHgfB0FbKRkg28MCDiwGCQIDAQABAoIBAENlqRJuaYCOg7Z/WyoyHAy5A/w2R1rs3ZaU3uEFtjE92T/c4bUbnfiMWTvegJMgdubqRmW4zWLksnOw5IU/KOqgizQu5LANzuU4NgFB87d2Wgb3ijcEi4HJ0ZUdIVNREJvLx21YGHDoNa+JmJW/+dU6LaT8Yg4471K4PzmEHtHTuDR2jmmSQ8Tw3Z18iqDB9Uatv9+3Bbi7aoB5b+x4z+h9udPAsxsC8+INrfF1i3r/QuScXASiGkhGixSsM9bcH7VhsZj3KT3KJBi1CZ4hZSsZV0cNd3f6nqAYO3A5FBzJkd1AqdynVJYrep8KyAlkuWRunykIkSPAUqi0iwwZ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Af3qGCjcSYSEMrTtZQ8xcwB0oJj4anmBuLnpOoQ7Pczei3Blk7+7KXa7g2kNWT11VqaDlLT9ete2LPRW3j6AygCL0UVM1TB2D7SQ44fu9YVulpltCL6qtp0b+opoicSPwpodavobZS7Gy2ukcFFpnbo9mcyKsyE1XfRy4evNWqpZudLv/vQntgWGMSUdG4mP5Cu5sodjLFDlQTvNNwQQIDAQAB';
      $aop->apiVersion = '1.0';
      $aop->signType = 'RSA2';
      $aop->postCharset='utf-8';
      $aop->format='json';
      $this->aop = $aop;
      
  }

    public function wappay($data){

       /*  $data['service'] = 'alipay.wap.create.direct.pay.by.user';
        $data['partner'] = $this->partner;//支付宝合作者身份ID 2088开头
        $data['seller_id'] = $data['partner'];//卖家支付宝账号
        $data['_input_charset'] = 'utf-8';
        $data['payment_type'] = '1';//1商品购买
        $data['app_pay'] = 'Y';
        $data['sign'] = $this->_getSign($data);
        $data['sign_type'] = 'MD5';//DSA、RSA、MD5三个值可选，必须大写。

        $params = http_build_query($data);
        $api_url = $this->api_url.$params;
        return $api_url; */

        $aop = $this->aop;
        $request = new \AlipayTradePagePayRequest ();
        $request->setNotifyUrl($data['notify_url']);
        $request->setReturnUrl($data['return_url']);
        $arr = [
          'out_trade_no' => $data['out_trade_no'],
          'product_code' => 'FAST_INSTANT_TRADE_PAY',
          'total_amount' => $data['total_fee'],
          'subject' => $data['subject'],
        ];
        $request->setBizContent(json_encode($arr));
        $result = $aop->pageExecute ($request); 
        return $result;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
          echo "成功";
        } else {
          echo "失败";
        }

    }

     /*
     * 验证回调的真实性
     */
    public function checkSignNew($data){
      require_once VENDOR_PATH.'alipaysdk/pagepay/service/AlipayTradeService.php';
      $aop = $this->aop;
      $config = array (	
        'app_id' => $aop->appId,
        'merchant_private_key' => $aop->rsaPrivateKey,
        'sign_type' => $aop->signType,
        'alipay_public_key' => $aop->alipayrsaPublicKey,
        'charset' => "UTF-8",
        'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
      );
      $alipaySevice = new \AlipayTradeService($config); 
      $result = $alipaySevice->check($data);
      return $result;
    }


     /**
     * 	作用：生成签名
     */
    protected function _getSign($Obj)
    {
        //过滤参数
        $Obj = $this->_paraFilter($Obj);
        
        foreach ($Obj as $k => $v)
        {
            $Parameters[$k] = $v;
        }
        //签名步骤一：按字典序排序参数
        ksort($Parameters);
        
        $String = $this->_createLinkstring($Parameters);

        //echo '【string1】'.$String.'</br>';
        //签名步骤二：在string后加入KEY
    
        $String = $String.$this->ALIPAY_PAYKEY;
        //echo "【string2】".$String."</br>";
        //签名步骤三：MD5加密

        $String = md5($String);

        return $String;
    }
    
    /**
     * 除去数组中的空值和签名参数
     * @param $para 签名参数组
     * return 去掉空值与签名参数后的新签名参数组
     */
    protected function _paraFilter($para) {
        $para_filter = array();
        while (list ($key, $val) = each ($para)) {
            if($key == "sign" || $key == "sign_type" || $val == "")continue;
            else	$para_filter[$key] = $para[$key];
        }
        return $para_filter;
    }
    
    
    /**
     * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param $para 需要拼接的数组
     * return 拼接完成以后的字符串
     */
    protected function _createLinkstring($para) {
    	$arg  = "";
    	while (list ($key, $val) = each ($para)) {
    		$arg.=$key."=".$val."&";
    	}
    	//去掉最后一个&字符
    	$arg = substr($arg,0,count($arg)-2);
    	
    	//如果存在转义字符，那么去掉转义
    	if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
    	
    	return $arg;
    }
    
    
    /*
     * 验证回调的真实性
     */
    public function checkSign($data){
        
        if(empty($data)) {//判断POST来的数组是否为空
            return false;
        }
        
        $sign = $this->_getSign($data);
        $post_sign = $data["sign"];
        if($sign == $post_sign){
            return true;
        }
        
        return false;
    }
    
    




}